package com.his.struts.action;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.AnswerForm;
import com.his.struts.form.UserForm;
import com.his.struts.form.ZydiseaseForm;
import com.his.struts.util.PageUtil;

/**
 * 中医功能类
 * @author liuyan
 *
 */
public class ZydiseaseAction extends DispatchAction {
	UsersDao dao = new UsersImp();
	/** 
	 * Method 取所有病例
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward allDis(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward  forward= null;
		HttpSession session = request.getSession();
		String admin = request.getParameter("admin");
		if(admin == null){
			admin = (String)request.getAttribute("admin");
		}
		int uid = 0;
		if(admin == null){
			UserForm uf = (UserForm)session.getAttribute("userinfo");
			uid = uf.getUid();
		}else{
			uid = (Integer)session.getAttribute("admin_uid");
		}
		String sql = "zybl where uid = "+uid;
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<ZydiseaseForm> list = dao.allzyDisease(rs);
			// 数据放到request中
			request.setAttribute("zybl", list);
		}
		
			if(admin == null){
				forward = mapping.findForward("sucessallDis");
			}else{
				forward = mapping.findForward("admin_sucessallDis");
			}
		return forward;
	}
	/**
	 * 删除病例
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward deleteDis(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int zyid = Integer.parseInt(request.getParameter("zyid"));
		UsersDao dao = new UsersImp();
		//根据zyid删除
		dao.deletezyDisease(zyid);
		request.setAttribute("message", "删除成功");
		return mapping.findForward("admin_deleteDis");
	}
	/**
	 * 添加中医病例
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addDis(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ZydiseaseForm zdf = (ZydiseaseForm)form;
		int zyuid = Integer.parseInt(request.getParameter("zyuid"));
		String ywlist = request.getParameter("zyyw");
		String [] yw = ywlist.split(",");
		List list = new ArrayList();
		for (int i = 0; i < yw.length; i++) {
			list.add(yw[i]);
		}
		zdf.setUid(zyuid);
		zdf.setZyyf(list);
		UsersDao dao = new UsersImp();
		boolean ok = dao.addzyDisease(zdf,1);
		if(ok){
			request.setAttribute("addok", "添加成功！！！！");
			request.setAttribute("message", "添加成功");
			return mapping.findForward("successadd");
		}else{
			return mapping.findForward("failadd");
		}
	}
	/**
	 * 编辑中医病例
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward editZyDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ZydiseaseForm zdf = (ZydiseaseForm)form;
		String ywlist = request.getParameter("zyyw");
		if(ywlist.startsWith("[")){
			ywlist = ywlist.substring(1);
		}
		if(ywlist.endsWith("]")){
			ywlist = ywlist.substring(0, ywlist.length() - 1);
		}
		String [] yw = ywlist.split(",");
		List list = new ArrayList();
		for (int i = 0; i < yw.length; i++) {
			list.add(yw[i]);
		}
		zdf.setZyyf(list);
		boolean ok = dao.editZyDisease(zdf);
		if(ok){
			request.setAttribute("admin", "ly");
			request.setAttribute("message", "修改成功");
			return mapping.findForward("successEdit");
		}else{
			return mapping.findForward("fail");
		}
	}
	/**
	 * Method execute 显示一个中医病历
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页 
	 */
	public ActionForward showZyDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String zyid = request.getParameter("zyid");
		if(zyid != null){
			int id  = Integer.parseInt(zyid);
			ZydiseaseForm zdf = dao.getZyDisease(id);
			request.setAttribute("oneZyDisease", zdf);
			return mapping.findForward("successShowZy");
		}else{
			return mapping.findForward("fail");
		}
	}
}